package com.lagou.niancheng.rpc.common.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 反射工具
 *
 * @author 大犬
 * @date 2020/4/27
 */
public class ReflectionUtil {
    private ReflectionUtil() {

    }

    public static List<Method> getAllMethods(Class type) {
        List<Method> methodList = new ArrayList<>();
        recursiveGetMethods(type, methodList);
        return methodList;
    }

    private static void recursiveGetMethods(Class type, List<Method> methodList) {
        if (type == Object.class || type == null) {
            return;
        }
        Method[] declaredMethods = type.getDeclaredMethods();
        if (declaredMethods == null || declaredMethods.length == 0) {
            return;
        }
        for (Method declaredMethod : declaredMethods) {
            declaredMethod.setAccessible(true);
            methodList.add(declaredMethod);
        }
        // 获取父类方法
        recursiveGetMethods(type.getSuperclass(), methodList);
        // 获取接口方法
        Class[] interfaces = type.getInterfaces();
        if (interfaces != null && interfaces.length != 0) {
            for (Class anInterface : interfaces) {
                recursiveGetMethods(anInterface, methodList);
            }
        }
    }
}
